#pragma once
#define NOMINMAX
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <iomanip>
#include <sstream>
#include <map>
#include <unordered_map>
#include "files.h"
#define SYSQUIT 1
#define SYSLOGIN 2
#define SYSSIGNIN 3
#define SYSCHECKALL 4
#ifndef _WINSOCK_DEPRECATED_NO_WARNINGS
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#endif
using namespace std;
using json = nlohmann::json;

// 消息类型枚举
enum MessageType {
	MSG_LOGIN_REQUEST = 0x01,
	MSG_LOGIN_RESPONSE = 0x81,
	MSG_SIGNIN_REQUEST = 0x02,
	MSG_SIGNIN_RESPONSE = 0x82,
	MSG_MAIN_MENU_REQUEST = 0x03,
	MSG_MAIN_MENU_RESPONSE = 0x83,
	MSG_CONSUMER_MENU_REQUEST = 0x04,
	MSG_CONSUMER_MENU_RESPONSE = 0x84,
	MSG_SELLER_MENU_REQUEST = 0x05,
	MSG_SELLER_MENU_RESPONSE = 0x85,
	MSG_DISPLAY_PRODUCTS_REQUEST = 0x06,
	MSG_DISPLAY_PRODUCTS_RESPONSE = 0x86,
	MSG_SEARCH_PRODUCTS_REQUEST = 0x07,
	MSG_SEARCH_PRODUCTS_RESPONSE = 0x87,
	MSG_ADD_PRODUCT_REQUEST = 0x08,
	MSG_ADD_PRODUCT_RESPONSE = 0x88,
	MSG_MANAGE_PRODUCTS_REQUEST = 0x09,
	MSG_MANAGE_PRODUCTS_RESPONSE = 0x89,
	MSG_DISPLAY_ACCOUNTS_REQUEST = 0x0A,
	MSG_DISPLAY_ACCOUNTS_RESPONSE = 0x8A,
	MSG_ADD_TO_CART_REQUEST = 0x0B,
	MSG_ADD_TO_CART_RESPONSE = 0x8B,
	MSG_MANAGE_CART_REQUEST = 0x0C,
	MSG_MANAGE_CART_RESPONSE = 0x8C,
	MSG_GENERATE_ORDER_REQUEST = 0x0D,
	MSG_GENERATE_ORDER_RESPONSE = 0x8D,
	MSG_MANAGE_ORDERS_REQUEST = 0x0E,
	MSG_MANAGE_ORDERS_RESPONSE = 0x8E,
	// 其他消息类型...
};

// 客户端状态结构
struct ClientState {
	sockaddr_in address;
	std::string userType;  // 用户类型字段
	int lastSequenceId = -1;
	time_t lastActivity = 0;
	std::string client_id;  // 客户端ID
	std::string loggedInUsername;  // 当前登录的用户名
	bool isLoggedIn = false;  // 登录状态
};

class eComSys {
private:
	//socket???
	WSADATA wsaData;
	SOCKET udpSocket;
	sockaddr_in serverAddr;

	// ???????
	userFileManager userManager;
	productFileManager productManager;
	orderFileManager orderManager;

	//??????
	long long width;              // ????????
	string divider;         // ??????
	vector<string> mainitems;   // ????????б?
	vector<string>consumeritems; //????????
	vector<string>selleritems; //??????
	string prompt;          // ?????????
	map<string, ShoppingCart> userCarts; // ?????????ID???????????

	//??????ò???s
	std::unordered_map<std::string, ClientState> usernameToClientState; // 用户名到客户端状态的映射
	int if_init = 0;
	//socket?????????
	bool initUDPSocket();
	// ??????в????
	std::string centerText(const string& text)const;

	// ????????
	std::string createDivider()const;

	//??????
	std::string modifyProduct(product* prod, const json& request);
public:
	// ??????
	eComSys(int w = 35, std::string div = "*", std::string p = "????????????");
	//????????
	~eComSys();

	//???з????
	void runServer();  // ????UDP??????

	// 报文处理函数
	void processPacket(const sockaddr_in& clientAddr, const char* buffer, int len);
	void sendResponse(const sockaddr_in& clientAddr, const std::string& payload);
	// ????????
	void addMainItem(const string& item);

	// ??????????
	std::string MainMenu(const json& request);

	// 新增：返回主菜单字符串
	std::string getMainMenuString();
	
	// 新增：处理主菜单请求
	std::string handleMainMenuRequest(const json& request);

	//??
	std::string signIn(const json& request);
	//??????
	std::string logIn(const json& request, const sockaddr_in& clientAddr);
	//????????????
	std::string displayAllAc(const json& request)const;

	//??????????
	std::string consumerMenu(const json& request);
	//????????
	std::string sellerMenu(const json& request);
	// ?????
	std::string displayProducts(const json& request) const;
	// ???????
	std::string searchProducts(const json& request) const;
	//???????
	std::string addProduct(const json& request);
	//???????
	std::string manageProducts(const json& request);
	//socket???
	//bool sendAll(SOCKET clientSocket, const char* data, int length);

	

private:
	// 获取客户端唯一标识
	std::string getClientKey(const sockaddr_in& addr);
	
	// 通过用户名获取或创建客户端状态
	ClientState& getClientStateByUsername(const std::string& username, const sockaddr_in& addr);
	
	// 通过client_id获取或创建客户端状态
	ClientState& getClientStateByClientId(const std::string& clientId, const sockaddr_in& addr);
	
	// 检查client_id是否已登录
	bool isClientLoggedIn(const std::string& clientId);
	
	// 通过client_id获取登录的用户名
	std::string getLoggedInUsernameByClientId(const std::string& clientId);
	
	// 强制登出指定client_id的所有登录
	void forceLogoutClient(const std::string& clientId);
	
	// 检查用户名是否已被登录
	bool isUsernameLoggedIn(const std::string& username);
	
	// 获取用户名的登录客户端ID
	std::string getClientIdByUsername(const std::string& username);
	
	// 通过用户名查找客户端地址
	sockaddr_in* getClientAddressByUsername(const std::string& username);
	
	// 更新用户名的客户端状态
	void updateClientState(const std::string& username, const sockaddr_in& addr, const std::string& userType = "");
	
	// 移除用户名的客户端状态
	void removeClientState(const std::string& username);
};